<?php
namespace app\admin\controller;

use app\common\service\AdminLogin;
use think\Controller;
use think\Request;

/**
 * @name 不认证控制器
 * @desc 不认证控制器
 * @auth false
 * @author: xujianping(814876569@qq.com)
 * @datetime: 2017/9/18 上午 10:37
 */
class Publics extends Controller
{

    private $adminLogin;

    public function __construct(Request $request, AdminLogin $adminLogin)
    {
        parent::__construct($request);
        $this->adminLogin = $adminLogin;
    }

    /**
     * @name 登录
     * @desc 登录页面
     * @auth false
     * @author: xujianping(814876569@qq.com)
     * @return mixed
     */
    public function login()
    {
        if ($this->request->isPost()) {

            $userName = $this->request->post('username','');
            $password = $this->request->post('password','');
            $remember = $this->request->post('remember','');

            //记住我
            $remember = $remember == 'on' ? 1 : 0;

            $loginStatus = $this->adminLogin->adminUserLogin($userName,$password);

            if ($loginStatus['status']){
                $this->success('登陆成功',$this->adminLogin->adminHomePage);
            }else{
                $this->error($loginStatus['message']);
            }

        } else {
//            $a = new \app\common\models\AdminUser();
//            $p = $a->setPassword('123456');
//            dump($p);
            if ($this->adminLogin->checkLogin()){
                $this->redirect($this->adminLogin->adminHomePage);
            }

            return $this->fetch();
        }
    }

    /**
     * @name 注销
     * @desc 注销登录页面
     * @auth false
     * @author: xujianping(814876569@qq.com)
     * @return mixed
     */
    public function logout()
    {
        $this->adminLogin->clearSession();
        $this->redirect('Publics/login');
    }

}